package com.zx.Ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.Ding.common.CustomException;
import com.zx.Ding.dto.DishDto;
import com.zx.Ding.entity.Dish;
import com.zx.Ding.entity.DishFlavor;
import com.zx.Ding.mapper.DishMapper;
import com.zx.Ding.service.DishFlavorService;
import com.zx.Ding.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        /*List<DishFlavor> flavorsList = new ArrayList<>();
        for ( DishFlavor list1 :flavors) {
            list1.setId(dtoId);
            flavorsList.add(list1);
            System.out.println(flavorsList);
        }*/
        //菜品口味
        //steam将集合转换成流 map将List<DishFlavor>转换成List<String>进行赋值 .collect(Collectors.toList())将流转换成集合类型
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        //因为flavors是集合 所以批量保存用到saveBatch
        dishFlavorService.saveBatch(flavors);

    }

    /*
    根据菜品查询对应的菜品信息和口味信息
    * */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询基本信息 从dish表中查询
        Dish dish = this.getById(id);

        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息,从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //请理当前菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据 --
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    //根据id批量删除删除菜品表
    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加批量删除条件
        queryWrapper.in(Dish::getId,ids);
        //查出所有被停售的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //如果有正在售卖的 必须先停售才能删除
        int count = this.count(queryWrapper);
        if (count>0){
            throw new CustomException("菜品正在售卖当中,需要停售才能售卖");
        }
        //可以删除
        this.removeByIds(ids);
    }
}
